package com.ym.pms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ym.pms.model.*;
import com.ym.pms.service.ApprovalnotesServices;
import com.ym.pms.service.RequisitionServices;
import com.ym.pms.utils.JSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ming
 * @since 2021-01-03
 */
@RestController
@Api(tags = "申请单接口")
@RequestMapping("/requisition")
public class RequisitionController {
    @Autowired
    private RequisitionServices requisitionServices;
    @Autowired
    private ApprovalnotesServices approvalnotesServices;

    @PostMapping
    @ApiOperation(value = "添加申请单",notes = "申请单id，时间，状态 由后端生成")
    public JSONResult add(Requisition requisition){
//        编码生成器？？？
//        requisition.setReqCoding("");
        requisition.setReqStatus(0);
        requisitionServices.save(requisition);
        return JSONResult.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id获取申请单")
    public JSONResult findAll(@PathVariable("id") Integer id){
        return JSONResult.ok(requisitionServices.getById(id));
    }

    @PreAuthorize("hasRole('PROPERTY')")
    @PostMapping("/WYsp")
    @ApiOperation(value = "物业管理员审批指定id的申请单",notes = "id, appReqstage由后端生成")
    public JSONResult WYsp(Approvalnotes approvalnotes){
        Requisition requisition = requisitionServices.getById(approvalnotes.getAppReqcoding());
        approvalnotes.setAppReqstage(1);
        if (approvalnotes.getAppIspass()==1) {
            requisition.setReqStatus(1);
        }else {
//               审批不通过
            requisition.setReqStatus(-1);
        }
        approvalnotesServices.save(approvalnotes);
        requisitionServices.updateById(requisition);
        return JSONResult.ok();
    }

    @PreAuthorize("hasRole('ARCHIVIST')")
    @PostMapping("/DAsp")
    @ApiOperation(value = "基础档案管理员审批指定id的申请单",notes = "id, appReqstage由后端生成")
    public JSONResult DAsp(Approvalnotes approvalnotes){
        Requisition requisition = requisitionServices.getById(approvalnotes.getAppReqcoding());
        approvalnotes.setAppReqstage(2);
        if (approvalnotes.getAppIspass()==1) {
            requisition.setReqStatus(2);
        }else {
//               审批不通过
            requisition.setReqStatus(-1);
        }
        approvalnotesServices.save(approvalnotes);
        requisitionServices.updateById(requisition);
        return JSONResult.ok();
    }

    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/XTsp")
    @ApiOperation(value = "系统管理员管理员审批指定id的申请单",notes = "id, appReqstage由后端生成")
    public JSONResult XTsp(Approvalnotes approvalnotes){
        Requisition requisition = requisitionServices.getById(approvalnotes.getAppReqcoding());
        approvalnotes.setAppReqstage(3);
        if (approvalnotes.getAppIspass()==1) {
            requisition.setReqStatus(3);
        }else {
//               审批不通过
            requisition.setReqStatus(-1);
        }
        approvalnotesServices.save(approvalnotes);
        requisitionServices.updateById(requisition);
        return JSONResult.ok();
    }



    @PreAuthorize("hasRole('PROPERTY')")
    @GetMapping("/WY")
    @ApiOperation("获取物业管理员有权审批的申请单列表")
    public JSONResult WYfindAll(Integer pageNum, Integer pageSize){
        QueryWrapper<Requisition> wrapper = new QueryWrapper<>();
        wrapper.eq("req_status",0);
        Page<Requisition> page = new Page<>(pageNum, pageSize);
        Page<Requisition> requisitions = requisitionServices.getBaseMapper().selectPage(page, wrapper);
        return JSONResult.ok(requisitions);
    }

    @PreAuthorize("hasRole('ARCHIVIST')")
    @GetMapping("/DA")
    @ApiOperation("获取基础档案有权审批的申请单列表")
    public JSONResult DAfindAll(Integer pageNum, Integer pageSize){
        QueryWrapper<Requisition> wrapper = new QueryWrapper<>();
        wrapper.eq("req_status",1);
        Page<Requisition> page = new Page<>(pageNum, pageSize);
        Page<Requisition> requisitions = requisitionServices.getBaseMapper().selectPage(page, wrapper);
        return JSONResult.ok(requisitions);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/XT")
    @ApiOperation("获取系统管理员有权审批的申请单列表")
    public JSONResult XTfindAll(Integer pageNum, Integer pageSize){
        QueryWrapper<Requisition> wrapper = new QueryWrapper<>();
        wrapper.eq("req_status",2);
        Page<Requisition> page = new Page<>(pageNum, pageSize);
        Page<Requisition> requisitions = requisitionServices.getBaseMapper().selectPage(page, wrapper);
        return JSONResult.ok(requisitions);
    }
}

